package searchteam.client.ui.widgets;

import java.util.ArrayList;
import java.util.Collection;

import searchteam.client.ui.SearchTeamUI;
import searchteam.client.ui.commands.Command;
import searchteam.client.ui.handlers.MirrorClickHandler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DataPanel extends HorizontalPanel implements Display{
	
	private VerticalPanel partieGauchePanel;
	private VerticalPanel listePanel;
	private VerticalPanel partieDroitePanel;
	private HorizontalPanel boutonsPanel;
	private ScrollPanel zoneScrollGauchePanel;
	private ScrollPanel zoneScrollDroitePanel;
	
	private Command refreshCommand;
	private Command createCommand;
	
	private Collection<MirrorDisplay> mirrors;
	private SearchTeamUI mainUI;
	
	public DataPanel(SearchTeamUI m, Command refreshC, Command createC) {
		
		refreshCommand = refreshC;
		createCommand = createC;
		mainUI = m;
		mirrors = new ArrayList<MirrorDisplay>();
		
		partieGauchePanel = new VerticalPanel();
		listePanel = new VerticalPanel();
		partieDroitePanel = new VerticalPanel();
		boutonsPanel = new HorizontalPanel();
		zoneScrollGauchePanel = new ScrollPanel(listePanel);
		zoneScrollDroitePanel = new ScrollPanel(partieDroitePanel);
		
		zoneScrollGauchePanel.setWidth("150px");
		zoneScrollGauchePanel.setHeight("300px");
		
		zoneScrollDroitePanel.setWidth("250px");
		zoneScrollDroitePanel.setHeight("300px");
		
		zoneScrollGauchePanel.setStyleName("panel-zoneScroll");
		partieDroitePanel.setStyleName("panel-partieDroite");
		listePanel.setStyleName("panel-liste");
		
		Button buttonAdd = new Button("Ajouter");
		buttonAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				createCommand.execute(DataPanel.this);
			}
		});
		boutonsPanel.add(buttonAdd);
		
		partieGauchePanel.add(boutonsPanel);
		partieGauchePanel.add(zoneScrollGauchePanel);
		this.add(partieGauchePanel);
		this.add(zoneScrollDroitePanel);
	}
	
	public void refresh() {
		refreshCommand.execute(this);
	}
	
	public void addMirror(MirrorDisplay mirror) {
		mirror.addClickHandler(new MirrorClickHandler(this, mirror));
		listePanel.add(mirror);
		mirrors.add(mirror);
	}
	
	public void clearMirrors() {
		listePanel.clear();
		mirrors.clear();
	}
	
	public void displayMirror(Mirror mirror) {
		if(contains(mirror)) {
			partieDroitePanel.clear();
			partieDroitePanel.add(mirror.display());
		}
		else {
			new InfoPopUp("L'element selectionne n'existe pas!", "Erreur");
			this.refresh();
		}
	}
	
	public boolean contains(Mirror mirror) {
		boolean result = false;
		
		if(mirrors.contains(mirror))
			result = true;
		
		for(MirrorDisplay m : mirrors) {
			if(m.getId() == mirror.getId() && m.getClass() == mirror.getClass())
				result = true;
		}
		
		return result;
	}
	
	public SearchTeamUI getMainUI() {
		return mainUI;
	}

}
